<?php
/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-14 0:17
 */

namespace App\Controller\User\Chat;

use App\Controller\User\BaseController;
use App\Middleware\AuthTokenMiddleware;
use App\Service\Chat\ChatRecordsService;
use App\Service\Chat\ChatSessionService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;


/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-14 0:17
 * @Middleware(AuthTokenMiddleware::class)
 * @ApiController(prefix="user/chat/chat-records")
 */
class ChatRecordsController extends BaseController
{

    /**
     * @Inject()
     * @var ChatRecordsService
     */
    protected $chatRecordsService;

    /**
     * @Notes:【】
     * @User: zwc
     * @Date: 2022-05-14 0:18
     * @PostApi(path="lists")
     * @FormData(key="receiver_id",rule="Required")
     */
    public function lists()
    {
        $data = $this->getValidatorData();

        $userId = $this->getUid();
        $data = $this->chatRecordsService->lists($userId, $data->get('receiver_id'));
        $data->transform(function ($item) use ($userId) {
            if ($userId == $item->user_id) {

            } else {

            }
            $user = $item->User;
//            dump($userId == $item->user_id, $user->toArray());
            $item->nickname = $user->nickname;
            $item->sender_id = $item->user_id;
            $item->avatar = $user->avatar;
            $item->content = $item->ChatMessage->content;
            $item->types = $item->ChatMessage->types;
            return $item;
        });
        $data->setCollection($data->sortKeysDesc()->values());
        _SUCCESS($data);
    }

    /**
     * @Notes:【】
     * @Date: 2022-06-17 13:05
     * @PostApi(path="read")
     * @FormData(key="user_id",rule="Required")
     */
    public function read()
    {
        $params = $this->getParams('receiver_id');
        $this->chatRecordsService->read($params);
        _SUCCESS();
    }

}